package com.hxexp.weixin.mvc.stationnc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.constant.StringConstant;
import com.hxexp.weixin.constant.TrafficlineNC;
import com.hxexp.weixin.hpe.api.arrivestations.model.ArriveStationsParamBean;
import com.hxexp.weixin.hpe.api.arrivestations.model.ArriveStationsResultBean;
import com.hxexp.weixin.hpe.api.startstations.model.StartStationsResultBean;
import com.hxexp.weixin.mvc.station.service.StationService;
import com.hxexp.weixin.mvc.trafficline.model.TicketQueryParam;

/**
 * 车站数据控制器 (当代运业集团需求相关)
 *
 * @author li.jiang
 *
 */
@Controller
@RequestMapping(value = "/stationnc")
public final class StationNCController {
    private static final Logger LOGGER = LoggerFactory.getLogger(StationNCController.class);
    private static final String TICKETQUERYPARAM_ATTR = "ticketQueryParam";
    private static final String ARRIVESTATIONS_ATTR = "arriveStations";
    @Autowired
    private StationService stationService;

    /**
     * 出发站列表页面
     *
     * @return 出发站列表
     */
    @RequestMapping(value = "/startlist")
    public String startStations(TicketQueryParam queryParam, Model model) {
        // 因为重新选择出发站，所以需要清除查询参数中的到达站信息，以便用户重新选择到达站
        queryParam.setDestination("");
        queryParam.setArriveCityId("");
        queryParam.setArriveStationId("");

        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        StartStationsResultBean startStationsResult = new StartStationsResultBean(); // HPE出发站列表返回结果
        try {
            startStationsResult = stationService.getStartStations(TrafficlineNC.GROUP_ID);
        } catch (ServiceException e) {
            LOGGER.error("调用出发站列表服务发生异常", e);
        }
        model.addAttribute("startStations", startStationsResult.getStartStations());

        return "start_stationsnc";
    }
    /**
     * 到达站列表页面
     *
     * @param TicketQueryParam
     *            车票查询请求参数
     * @return 到达站列表
     */
    @RequestMapping(value = "/arrivelist")
    public String arriveStations(TicketQueryParam queryParam, Model model) {
        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        model.addAttribute(ARRIVESTATIONS_ATTR, queryArriveStations(queryParam).getArriveStations());

        return "arrive_stationsnc";
    }

    /**
     * 到达站列表片段（根据到达站名称模糊查询时使用）
     *
     * @param queryParam
     * @param model
     * @return
     */
    @RequestMapping(value = "/ajaxArrivelist")
    public String getArriveList(TicketQueryParam queryParam, Model model) {
        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        model.addAttribute(ARRIVESTATIONS_ATTR, queryArriveStations(queryParam).getArriveStations());

        return "arrive_station_fragmentnc";
    }

    // 查询到达站
    private ArriveStationsResultBean queryArriveStations(TicketQueryParam queryParam) {
        ArriveStationsResultBean arriveStationsResult = new ArriveStationsResultBean();// HPE到达站列表返回结果

        ArriveStationsParamBean arriveStationsParam = new ArriveStationsParamBean();
        arriveStationsParam.setStartCityId(StringConstant.BLANK);
        arriveStationsParam.setStartStationId(StringConstant.BLANK);
        arriveStationsParam.setArriveStationName(queryParam.getArriveStationName());
        arriveStationsParam.setGroupId(TrafficlineNC.GROUP_ID);
        arriveStationsParam.setArriveStationType(TrafficlineNC.SJ);

        try {
            arriveStationsResult = stationService.getArriveStations(arriveStationsParam);
        } catch (ServiceException e) {
            LOGGER.error("调用到达站列表服务发生异常", e);
        }

        return arriveStationsResult;
    }

}
